define(["Zepto", 
        "underscore", 
        "Backbone", 
        "Handlebars",  
        "text!templates/pagep/teacher.html",
        "collections/corsocollection",
        "views/teacher/coursesview",
        "views/common/menuview"
], function($, _, Backbone, Handlebars, template, CorsoCollection, CoursesView, MenuView) {
	
   
	  var TeacherPageP = Backbone.View.extend({
		  
	        template: Handlebars.compile(template),

	        render: function(eventName) {
	        	
	        	this.$el.html(this.template(this.model.toJSON()));
	        	
	        	// menu top
	        	this.menutop = new MenuView({
	               	el : $('#menu', this.el),
	    			model : this.model
				});
	            this.menutop.render();
	            
	            this.corsoCollection = new CorsoCollection();
				this.corsoCollection.fetch({
					query : {
						isattivo: true,
						docente: {"__type":"Pointer","className":"_User","objectId":this.model.get('objectId')}
	               	},
	               	success: function(data) {
	                	this.courses = new CoursesView({
							el : $('#corsi ul', this.el),
							model : data
	    				});
	                	this.courses.render();
						var scroll1 = new iScroll('corsi',
                                  { useTransition:false,
                                    checkDOMChanges: true,
                                    hideScrollbar: true,
                                    fixedScrollbar:true }
                                );
					}});
				
				this.pastcorsoCollection = new CorsoCollection();
				this.pastcorsoCollection.fetch({
					query : {
						isattivo: false,
						docente: {"__type":"Pointer","className":"_User","objectId":this.model.get('objectId')}
	               	},
	               	success: function(d) {
	                	this.pastcourses = new CoursesView({	    				
							el : $('#pastcorsi ul', this.el),
							model : d
	    				});
	                	this.pastcourses.render();
						var scroll2 = new iScroll('pastcorsi',
                                  { useTransition:false,
                                    checkDOMChanges: true,
                                    hideScrollbar: true,
                                    fixedScrollbar:true }
                                );
					}});
				
				if(this.pastcorsoCollection.length < 1){
					$('#pastcorsi').remove();
				}else if(corsoCollection.length < 1){
					$('#corsi').remove();
				}
				
				/*this.pastcorsoCollection = new CorsoCollection();
				_.each(this.corsoCollection.models, function(c) {
					  if(!c.get('isattivo')){
						  alert('inattivo' + c.get('nome'));
						  this.pastcorsoCollection.add(c);
					  }
					}, this);
				alert('len ' + this.pastcorsoCollection.length);
				if(this.pastcorsoCollection.length > 0){
					this.pastcourses = new CoursesView({	    				
						el : $('#pastcorsi ul', this.el),
						model : this.pastcorsoCollection
    				});
                	this.pastcourses.render();
				}*/
				
	            return this;
	        },
	        

	        events: {
	            'touchstart .button': 'over',
	            'touchend .button': 'left',
	            'touchcancel .button': 'left',
	            'touchstart button': 'over',
	            'touchend button': 'left',
	            'touchcancel button': 'left',
	            'touchstart .ac-container article.selectable ul li': 'over',
	            'touchend .ac-container article.selectable ul li': 'left',
	            'touchcancel .ac-container article.selectable ul li': 'left'
	        },

	      

	        over: function(event) {
	            $(event.currentTarget).addClass('fake-active');
	        },

	        left: function(event) {
	            $(event.currentTarget).removeClass('fake-active');
	        }

	});

    return TeacherPageP;

});

